<?php

namespace app\api\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * 订单详情
 */
class OrderItem extends Model
{
    // protected static function base($query)
    // {
    //     $query->where('status',1);
    // }

    // protected $name='mall_goods';

    // 开启自动写入时间戳字段
    protected $auto_timestamp = 'false';

    // 设置json类型字段
    // protected $json = ['sf'];

    // 追加属性

    use SoftDelete;

    protected $deleteTime = 'delete_time';

    public function getSfAttr($value)
    {
        //  foreach($orderItem as $key1 => $v) {
        if ($value) {
            $sf_info_temp = json_decode($value, true);
            foreach ($sf_info_temp as $key => $value) {
                $body = array();
                for ($i = 0; $i < count($value['type_name']); $i++) {
                    $body[$i]['type_name'] = $value['type_name'][$i];
                    $body[$i]['value'] = $value['value'][$i];
                }
                $sf_info_temp[$key]['body'] = $body;
            }
            return $sf_info_temp;
        } else {
            return [];
        }
       
        // $orderItem[$key1]['sf'] = $sf_info_temp;
        // }
    }

    public function createItem($goodsInfo, $oid, $sku)
    {
        $data = [
            'title' => $goodsInfo['title'],
            'logo' => $goodsInfo['logo'],
            'order_id' => $oid,
            'gid' => $goodsInfo['id'],
            'pid' => $sku['id'],
            'servie_price' => $sku['service_fee'],
            'type' => $goodsInfo['type'],
            'sku_title' => $sku['title'],
            'sf' => $sku['sf'],
        ];
        $this->save($data);
    }
}
